package com.zhihuichongcheng.application.ui.mine;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.smartlib.cmnObject.util.DialogUtil;
import com.smartlib.cmnObject.util.ToastOpt;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;

import java.util.HashMap;

/**
 * Created by 3water on 2016/10/29.
 */

public class ChangePasswordActivity extends BaseActivity {

    private EditText mEtOldPassword,mEtNewPassword,mEtEnterAgain;
    private TextView mTvOk;
    private UserBean mUser;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        initView();
        initData();
    }

    private void initView()
    {
        setArrowTitle("返回");
        setTitle("修改密码");
        setStatusBarLightMode();
        mEtOldPassword = (EditText)findViewById(R.id.et_oldPassword);
        mEtNewPassword = (EditText)findViewById(R.id.et_newPassword);
        mEtEnterAgain = (EditText)findViewById(R.id.et_enterAgain);
        mTvOk = (TextView)findViewById(R.id.tv_ok);
        mTvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oldPassword = mEtOldPassword.getText().toString();
                String newPassword = mEtNewPassword.getText().toString();
                String enterAgain = mEtEnterAgain.getText().toString();
                if(!TextUtils.isEmpty(newPassword)&&!TextUtils.isEmpty(oldPassword)&&!TextUtils.isEmpty(enterAgain)&&newPassword.equals(enterAgain))
                {
                    DialogUtil.showLoadDiadlog(mContext);
                    HashMap<String,String> params = new HashMap<String, String>();
                    params.put("oldPassword",oldPassword);
                    params.put("newPassword",newPassword);
                    params.put("token",mUser.getToken());
                    mManager.post(NetworkAPI.ChangePassword, new HttpCallback() {
                        @Override
                        public void onSuccess(int code, String value,String msg) {
                            DialogUtil.dismiss(mContext);
                            if(code ==1)
                            {
                                app.loginOut();
                                ToastOpt.createToast(mContext,msg);
                                app.setUser(null);
                                spUtil.putString("user","");
                                startActivity(LoginActivity.class);
                                MiPushClient.setAlias(mContext,"",null);
                                finish();
                            }
                            else
                            {
                                ToastOpt.createToast(mContext,msg);
                            }

                        }

                        @Override
                        public void onFailure(String value) {
                            DialogUtil.dismiss(mContext);
                        }
                    },params);
                }
                else if(TextUtils.isEmpty(oldPassword))
                {
                    ToastOpt.createToast(mContext,"原密码为空");
                }
                else {
                    ToastOpt.createToast(mContext,"2次新密码输入的不一致");
                }
            }
        });

        mEtOldPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length()>0)
                {
                    String enterPassword = mEtEnterAgain.getText().toString();
                    String newPassword = mEtNewPassword.getText().toString();
                    if(!TextUtils.isEmpty(enterPassword)&&!TextUtils.isEmpty(newPassword))
                    {
                        mTvOk.setEnabled(true);
                        mTvOk.setTextColor(getResources().getColor(R.color.white));
                    }
                    else
                    {
                        mTvOk.setEnabled(false);
                        mTvOk.setTextColor(getResources().getColor(R.color.color_50_white));
                    }
                }
                else
                {
                    mTvOk.setEnabled(false);
                    mTvOk.setTextColor(getResources().getColor(R.color.color_50_white));
                }
            }
        });

        mEtNewPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length()>0)
                {
                    String oldPassword = mEtOldPassword.getText().toString();
                    String enterPassword = mEtEnterAgain.getText().toString();
                    if(!TextUtils.isEmpty(oldPassword)&&!TextUtils.isEmpty(enterPassword))
                    {
                        mTvOk.setEnabled(true);
                        mTvOk.setTextColor(getResources().getColor(R.color.white));
                    }
                    else
                    {
                        mTvOk.setEnabled(false);
                        mTvOk.setTextColor(getResources().getColor(R.color.color_50_white));
                    }
                }
                else
                {
                    mTvOk.setEnabled(false);
                    mTvOk.setTextColor(getResources().getColor(R.color.color_50_white));
                }
            }
        });

        mEtEnterAgain.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length()>0)
                {
                    String oldPassword = mEtOldPassword.getText().toString();
                    String newPassword = mEtNewPassword.getText().toString();
                    if(!TextUtils.isEmpty(oldPassword)&&!TextUtils.isEmpty(newPassword))
                    {
                        mTvOk.setEnabled(true);
                        mTvOk.setTextColor(getResources().getColor(R.color.white));
                    }
                    else
                    {
                        mTvOk.setEnabled(false);
                        mTvOk.setTextColor(getResources().getColor(R.color.color_50_white));
                    }
                }
                else
                {
                    mTvOk.setEnabled(false);
                    mTvOk.setTextColor(getResources().getColor(R.color.color_50_white));
                }
            }
        });

    }

    private void initData()
    {
        mUser = app.getUser();
    }
}
